import QtQuick 1.1
import com.meego 1.0
import "CommomUtil.js" as Util
import "Modulelib.js" as ModuleUtil
import "database.js" as DBUtil

Page {
    id: module_config_page

    tools: ModuleInfoToolBar{
        id: toolbar_info
        isExite: module_info_page.isFavorited
        onFavorited:{
            if(module_info_page.isFavorited){
                favorites_dialog.open();
            }else{
                DBUtil.addCarFavorite(ModuleUtil.current_series_id,ModuleUtil.current_series_name,module_info_page.infoImage,
                                      module_info_page.seriesName, vehicles_price.text);
                module_info_page.isFavorited = true
            }
        }
    }

    orientationLock : PageOrientation.LockPortrait


    TitleBar{
           id: module_config_title
           anchors.top: module_config_page.top; anchors.left: module_config_page.left
           width: module_config_page.width; height: 71
           titleName: ModuleUtil.current_series_name
    }


    ConfirmDialog{
        id: favorites_dialog
        diaContent: "您将把该车型从收藏夹中删除?"
        onAccepted: {
                DBUtil.removeFavorite(ModuleUtil.current_series_id,ConstUtil.FavoriteType.Car);
                module_info_page.isFavorited = false
        }
    }

    ListModel{
        id: config_data
    }

    Rectangle{
        id: config_desc_rect
        width: parent.width; height: 55
        color: "#bdbee2"
        border{color: "white"; width: 1}
        anchors.left: parent.left; anchors.top: module_config_title.bottom
        z: 10
        Row{
            Item{
                width: 50; height: 50
            }

            Text {
                id: config_desc
                text: "查看详细参数"
                width: 430; height: 50
                color: "#000000"
                font{bold: true; pointSize: 17}
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignLeft
            }
        }
    }

    Component{
        id: configShowDelegate
        Item {
            width: 450; height: 35
            Rectangle{
                anchors.fill: parent
                color: index % 2 === 0 ? "#bbc5d5" : "#eef7f7"
                Row{
                    spacing: 2
                    Item{
                        width: 20; height: 35
                    }
                    Text {
                        text: conName + ":"
                        color: "#878d84"
                        width: 170; height: 35
                        font{bold: true; pointSize: 14}
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignLeft
                    }
                    Text {
                        text: conContent
                        color: "#344491"
                        width: 240; height: 35
                        font{bold: true; pointSize: 15}
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignLeft
                    }
                }
            }
        }
    }

    Component{
        id: configHeaderDelegate
        Item{
            width: 450; height: 45
            Rectangle{
                anchors.fill: parent
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "white" }
                    GradientStop { position: 0.5; color: "#c0c5c6" }
                    GradientStop { position: 1.0; color: "white" }
                }
                border{color: "#c0c5c6"; width: 1}
                Row{
                    Item{
                        width: 30; height: 45
                    }
                    Text{
                        color: "#344491"
                        text: section
                        width: 420; height: 45
                        font{bold: true; pointSize: 17}
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                    }
                }
            }
        }
    }

    Rectangle{
        id: cars_list_rect
        width: parent.width; height: parent.height - 127
        anchors{left: parent.left; top: config_desc_rect.bottom;}
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 1; color: "#c0c5c6" }
        }
        Rectangle{
            anchors{fill: parent; leftMargin: 15; rightMargin: 15}
            ListView{
                id: cars_list
                anchors.fill: parent
                model: config_data
                delegate: configShowDelegate

                section.property: "conType"
                section.criteria: ViewSection.FullString
                section.delegate: configHeaderDelegate
            }
        }
    }

    BusyLoading{
        id: loading
        anchors.centerIn: parent
        visible:false
    }

    Component.onCompleted: {
        loading.loading();
        module_config_page.getBaseData();
    }

    function getBaseData(){
        Util.getXmlStringByUrl(ModuleUtil.getSpecConfigUrl(),baseCallback);
    }

    function baseCallback(responseXml){
        var a = responseXml.documentElement;
        var conType = ""; var conName = ""; var conContent = "";
        for(var i=0;i<a.childNodes[0].childNodes.length; ++i){
            if(a.childNodes[0].childNodes[i] !== null && a.childNodes[0].childNodes[i].childNodes.length > 6){
                conType = a.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
                conName = a.childNodes[0].childNodes[i].childNodes[3].childNodes[0].nodeValue;
                conContent = a.childNodes[0].childNodes[i].childNodes[5].childNodes[0].nodeValue;
                config_data.append({"conType":conType, "conName":conName, "conContent":conContent});
            }
        }
        getOptionData(); // 基本参数加载完成后,再加载配置参数
    }

    function getOptionData(){
        Util.getXmlStringByUrl(ModuleUtil.getSpecConfigUrl(),optionCallback);
    }

    function optionCallback(responseXml){
        var a = responseXml.documentElement;
        var conType = ""; var conName = ""; var conContent = "";
        for(var i=0;i<a.childNodes[0].childNodes.length; ++i){
            if(a.childNodes[0].childNodes[i] !== null && a.childNodes[0].childNodes[i].childNodes.length > 6){
                conType = a.childNodes[0].childNodes[i].childNodes[1].childNodes[0].nodeValue;
                conName = a.childNodes[0].childNodes[i].childNodes[3].childNodes[0].nodeValue;
                conContent = a.childNodes[0].childNodes[i].childNodes[5].childNodes[0].nodeValue;
                config_data.append({"conType":conType, "conName":conName, "conContent":conContent});
            }
        }
        loading.finish();
    }
}
